package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.efounder.constant.EnvironmentVariable;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.MyAirdropBean;
import com.pansoft.openplanet.fragment.MyAirDropAdapter;
import com.pansoft.openplanet.util.TCRequestUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.utilcode.util.ToastUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 我的空投
 * Created by yqs on 2018/07/27
 */

public class MyAirDropActivity extends TCBaseActivity implements View.OnClickListener {
    public static final String TAG = "MyAirDropActivity";

    private SmartRefreshLayout refreshLayout;
    private RecyclerView recycleview;
    private RelativeLayout title;
    private ImageView ivBack;
    private ImageView ivClose;
    private TextView tvTitle;
    private ImageView ivAdd;
    private ImageView ivSearch;
    private ImageView ivDetail;
    private TextView tvSave;
    private EditText etInput;

    private List<MyAirdropBean> mDatas;
    private int page = 1;
    private MyAirDropAdapter mAdapter;


    public static void start(Context context) {
        Intent starter = new Intent(context, MyAirDropActivity.class);
        //starter.putExtra("model", model);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_planet_activity_my_airdrop);
        initView();
        initListener();
        initData();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dispose();
    }


    private void initView() {
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        recycleview = (RecyclerView) findViewById(R.id.recycleview);
        title = (RelativeLayout) findViewById(com.efounder.chat.R.id.title);
        ivBack = (ImageView) findViewById(com.efounder.chat.R.id.iv_back);
        ivClose = (ImageView) findViewById(com.efounder.chat.R.id.iv_close);
        tvTitle = (TextView) findViewById(com.efounder.chat.R.id.tv_title);
        ivAdd = (ImageView) findViewById(com.efounder.chat.R.id.iv_add);
        ivSearch = (ImageView) findViewById(com.efounder.chat.R.id.iv_search);
        ivDetail = (ImageView) findViewById(com.efounder.chat.R.id.iv_detail);
        tvSave = (TextView) findViewById(com.efounder.chat.R.id.tv_save);
        tvSave.setOnClickListener(this);

    }

    private void initListener() {
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page += 1;
                loadData(page, true);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (mDatas != null) {
                    mDatas.clear();
                    mAdapter.notifyDataSetChanged();
                    page = 1;
                    loadData(page, false);
                }
            }
        });
    }

    private void initData() {
        tvTitle.setText(R.string.open_planet_my_airdrop_field);
        // tvSave.setVisibility(View.VISIBLE);
        mDatas = new ArrayList<>();
        mAdapter = new MyAirDropAdapter(this, mDatas);
        recycleview.setAdapter(mAdapter);
        loadData(1, false);
    }

    private void loadData(int page, boolean isLoadMore) {
//        for (int i = 0; i < page * 10; i++) {
//            AirDropModel model = new AirDropModel.Builder()
//                    .icon(R.drawable.wechat_icon_launcher)
//                    .name("IPCom星际战队")
//                    .endTime("")
//                    .assetName("能量币")
//                    .progress(60)
//                    .title("数字货币风暴的来临，快来免费空投！")
//                    .state(AirDropModel.STATE_STARTED)
//                    .build();
//            mDatas.add(model);
//        }
//        mDatas.get(1).setState(AirDropModel.STATE_STOP);
//        mDatas.get(1).setProgress(100);
//        mDatas.get(2).setState(AirDropModel.STATE_UNSTART);
//        mDatas.get(2).setProgress(0);
        HashMap<String, String> map = new HashMap<>();
        map.put("userId", EnvironmentVariable.getUserName());

        TCRequestUtil.getCommonRequest(TAG, "aridrop/getUserAirdrop", map, new TCRequestUtil.TCRequestCallback() {
            @Override
            public void onSuccess(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    //{"result":"success","data":7.04175}

//                    {"result":"success","data":[{"address":"0x5941166c7db099173a293e5711a57c8b81351fb1","airdropId":19,
//                            "coinName":"ewq","organization":"qwe",
//                            "picture":"http://panserver.solarsource.cn:9692/panserver/files/4fd4cbf8-3893-4496-8660-2348232ea7b5/download",
//                            "time":1533820202000,"tokenNumber":0.76062,"userId":"18363825829"}]}
                    if ("success".equals(jsonObject.optString("result"))) {
                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                        if (jsonArray != null) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject item = jsonArray.getJSONObject(i);
                                MyAirdropBean bean = new MyAirdropBean.Builder().address(item.optString("address"))
                                        .airdropId(item.optInt("airdropId"))
                                        .coinName(item.optString("coinName"))
                                        .organization(item.optString("organization"))
                                        .picture(item.optString("picture"))
                                        .time(item.optLong("time"))
                                        .tokenNumber(item.getDouble("tokenNumber"))
                                        .build();
                                mDatas.add(bean);
                            }
                        }
                        mAdapter.notifyDataSetChanged();

                    } else {
                        ToastUtils.showShort(R.string.common_text_http_request_fail);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtils.showShort(R.string.common_text_http_request_fail);
                }

            }

            @Override
            public void onFail(String error) {
                ToastUtils.showShort(R.string.common_text_http_request_fail);
            }
        });
        if (isLoadMore) {
            refreshLayout.finishLoadMore(100);
        } else {
            refreshLayout.finishRefresh(100);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {


        }
    }


}